<?php
namespace Admin\Controller;

use Think\Page;

class TagController extends CommonController
{
    public $model = null;
    
    public function __construct()
    {
        parent::__construct();
        $this->model = M('admin_example');
    }

    /**
     * 数据列表
     * @return [type]
     */
    public function index()
    {
        $where = [];
        // 查找int条件
        $id = I('get.id', 0, 'intval');
        if ($id) {

            $where['id']  = $id;
            $fields['id'] = $id;
            $this->assign('id', $id);
        }

        // 查找varchar条件
        $varchar = I('get.varchar', '', 'trim');
        if ($varchar) {

            $where['e_varchar'] = ['LIKE', '%' . $varchar . '%'];
            $fields['varchar']  = $varchar;
            $this->assign('varchar', $varchar);
        }

        // 查找date条件 / datetime条件
        $datetime_start = I('get.datetime_start', '', 'trim');
        $datetime_end   = I('get.datetime_end', '', 'trim');
        if ($datetime_start || $datetime_end) {

            $where['e_datetime'] = ['between', [$datetime_start, $datetime_end]];

            if (!$datetime_start) {
                $where['e_datetime'] = ['elt', $datetime_end];
            }

            if (!$datetime_end) {
                $where['e_datetime'] = ['egt', $datetime_start];
            }

            $fields['datetime_start'] = $datetime_start;
            $fields['datetime_end']   = $datetime_end;

            $this->assign('datetime_start', $datetime_start);
            $this->assign('datetime_end', $datetime_end);
        }

        $count = $this->model->where($where)->count();
        $this->assign('count', $count);

        $pages = new Page($count, C('PAGE'), $fields);
        $this->assign('pages', $pages->show());

        $datas = $this->model->where($where)
            ->order('id desc')
            ->limit($pages->firstRow . ',' . $pages->listRows)
            ->select();
        $this->assign('datas', $datas);
        $this->assign('bread_crumb', $bread_crumb = generate_bread_crumb([
            '系统设置',
            '开发示例',
        ]));
        $this->display();
    }

    /**
     * 添加数据，添加完修改
     */
    public function add()
    {
        if (IS_GET) {

            $this->assign('select', D('Admin/Html', 'Service')->exampleSelect());
            $this->assign('radio', $radio = D('Admin/Html', 'Service')->exampleRadio());
            $User = M("category");
            $category=$User->select();
            $this->assign("category", $category);

            $this->display();

        } elseif (IS_POST) {
            $data = [
                'e_varchar'  => I('post.e_varchar', '', 'trim'),
                'e_decimal'  => I('post.e_decimal', 0, 'floatval'),
                'e_select'   => I('post.e_select', 0, 'intval'),
                'e_min_text' => I('post.e_min_text', '', 'trim'),
                'e_text'     => I('post.e_text', '', 'trim'),
                'e_datetime' => I('post.e_datetime', '', 'trim'),
                'e_is'       => I('post.e_is','0','int'),
                'order_type' => I('post.order_type','','trim'),
                'out_href'       => I('post.out_href','','trim'),
                'making_type' => I('post.making_type','','trim'),
                'video_type' => I('post.video_type','','trim'),
                'pv_type' => I('post.pv_type','','trim')
            ];

            // if ($data['e_is'] == 'on') {
            //     $data['e_is'] = 1;
            // } else {
            //     $data['e_is'] = 0;
            // }
            
            // 首字母大写，下划线
            // 如：Ad  Ad_Banner（Uploads文件夹名称）
            $result = upload('Example');

            $id = $this->model->add($data);

            if (isset($result['e_image'])) {
                $this->model
                    ->where(['id' => $id])
                    ->save(['e_image' => $result['e_image']['url']]);
            }

            if ($id) {
                $this->success('添加成功', U(CONTROLLER_NAME . '/update', [
                    'id' => $id,
                ]));
            }
        }
    }

    public function update()
    {
        if (IS_GET) {

            $id = I('get.id', 0, 'intval');

            $data = $this->model->find($id);
            
            $User = M("category");
            $category=$User->select();
            $this->assign("category", $category);
            
            $this->assign('data', $data);
            
            $on_id = M("admin_example");
            $selected =$on_id->select();
            $this->assign("selected", $selected);
            
            $this->assign('select', D('Admin/Html', 'Service')->exampleSelect($data['e_select']));
            $this->assign('radio', $radio = D('Admin/Html', 'Service')->exampleRadio($data['e_select']));
            // dump($radio);exit;
            $this->display();

        } elseif (IS_POST) {

            $id   = I('post.id', 0, 'intval');
            $data = [
                'e_varchar'  => I('post.e_varchar', '', 'trim'),
                'e_decimal'  => I('post.e_decimal', 0, 'floatval'),
                'e_select'   => I('post.e_select', 0, 'intval'),
                'e_min_text' => I('post.e_min_text', '', 'trim'),
                'e_text'     => I('post.e_text', '', 'trim'),
                'e_date'     => I('post.e_date', '', 'trim'),
                'e_datetime' => I('post.e_datetime', '', 'trim'),
                'e_is'       => I('post.e_is','0','int'),
                'href'       => I('post.href','','trim'),
                'out_href'       => I('post.out_href','','trim'),
                'order_type' => I('post.order_type','','trim'),
                'making_type' => I('post.making_type','','trim'),
                'video_type' => I('post.video_type','','trim'),
                'pv_type' => I('post.pv_type','','trim')
            ];
            // 首字母大写，下划线
            // 如：Ad  Ad_Banner （Uploads文件夹名称）
            $result = upload('Example');
            $this->model->where(['id' => $id])->save($data);

            if (isset($result['image'])) {
                $this->model
                    ->where(['id' => $id])
                    ->save(['e_image' => $result['image']['url']]);
            }

            $this->success('修改成功', U(CONTROLLER_NAME . '/update', [
                'id' => $id,
            ]));
        }
    }

    /**
     * delete 方法在CommonController
     */

}
